<script setup>
import { ref, nextTick } from 'vue'
import api from '@/api'
import uploadImage from '@/components/uploadImage.vue'
import { Modal } from '@arco-design/web-vue'
import { parsePage, parseQuery } from '@/utils'
import { useRoute, useRouter } from 'vue-router'
import { transformDuration } from '@/utils'

const $route = useRoute()
const $router = useRouter()

const visible = ref(false)
const user = ref()
const listFormData = ref({
  page: parsePage($route.query.page, 1),
})
const listData = ref([])
const listTotal = ref(0)
const listLoading = ref(false)

const getListData = async (page = 1) => {
  listLoading.value = true
  try {
    listFormData.value.page = page
    const query = parseQuery(listFormData.value)
    const res = await api.get(`users/${user.value.id}/watch`, {
      params: query,
    })
    listData.value = res.data
    listTotal.value = res.total
  } catch (error) {
    console.error(error)
  }
  listLoading.value = false
}

const show = (item) => {
  user.value = item
  visible.value = true
  getListData()
}

defineExpose({ show })
</script>

<template>
  <a-modal
    v-model:visible="visible"
    title="观看记录"
    :footer="false"
    width="1200px"
    body-style="padding:0"
  >
    <a-table
      :loading="listLoading"
      @page-change="getListData"
      class="table"
      :data="listData"
      :pagination="{
        simple: true,
        showTotal: true,
        size: 'small',
        total: listTotal,
        pageSize: 20,
        current: listFormData.page,
      }"
      table-layout-fixed
    >
      <template #columns>
        <a-table-column title="序号" :width="60">
          <template #cell="{ rowIndex }"> {{ rowIndex + 1 }} </template>
        </a-table-column>
        <a-table-column title="课程" width="30%">
          <template #cell="{ record }">
            <div class="cell-course">
              <div class="image">
                <img :src="record.course.image" />
              </div>
              <div class="content">
                <div class="label">{{ record.course.name }}</div>
                <div class="description">{{ record.course.description }}</div>
              </div>
            </div>
          </template>
        </a-table-column>
        <a-table-column title="章节">
          <template #cell="{ record }">
            {{ record.section.order }}. {{ record.section.name }}
            <!-- <div class="cell-section">
              <img class="poster" :src="record.section.image_url" />
              <div class="label">
                {{ record.section.order }}. {{ record.section.name }}
              </div>
            </div> -->
          </template>
        </a-table-column>
        <a-table-column title="时长" :width="100">
          <template #cell="{ record }">
            {{ transformDuration(record.section.duration) }}
          </template>
        </a-table-column>
        <a-table-column
          title="观看时间"
          data-index="start_at"
          :width="160"
        ></a-table-column>
      </template>
    </a-table>
  </a-modal>
</template>

<style lang="scss" scoped>
.cell-course {
  display: flex;

  .image {
    width: 144px;
    height: 81px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    img {
      display: block;
      max-width: 100%;
      max-height: 100%;
    }
  }
  .content {
    padding-left: 5px;
    .title {
      line-height: 1;
    }

    .description {
      font-size: 12px;
      color: rgba(0, 0, 0, 0.5);
      margin-top: 5px;
      line-height: 1.6;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      height: 36px;
      white-space: pre;
    }
  }
}

.cell-section {
  width: 108px;
  height: 192px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;

  img {
    max-width: 100%;
    max-height: 100%;
    display: block;
  }

  .label {
    position: absolute;
    left: 0;
    bottom: 0;
    padding: 5px;
    color: rgb(96, 98, 102);
  }
}
</style>
